import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { PerPartTimeApi } from '#/api/per/partTime';
import type { DescriptionItemSchema } from '#/components/description';

import { h } from 'vue';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';
import { formatDate } from '@vben/utils';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'organizationName',
      label: '单位名称',
      component: 'Input',
      componentProps: {
        placeholder: '请输入社会兼职单位名称',
      },
    },
    {
      fieldName: 'organizationType',
      label: '单位性质',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.PER_EMPLOYER_TYPE),
        placeholder: '请选择单位性质',
      },
    },
    {
      fieldName: 'position',
      label: '担任职务',
      component: 'Input',
      componentProps: {
        placeholder: '请输入担任职务',
      },
    },
    {
      fieldName: 'paymentStatus',
      label: '取酬情况',
      component: 'Input',
      componentProps: {
        placeholder: '请选择取酬情况',
      },
    },
    {
      fieldName: 'partTimeDate',
      label: '兼职时间',
      component: 'DatePicker',
      componentProps: {
        showTime: false,
        format: 'YYYY-MM-DD',
        valueFormat: 'x',
      },
    },
    {
      fieldName: 'approvedDepartment',
      label: '批准部门',
      component: 'Input',
      componentProps: {
        placeholder: '请输入批准部门',
      },
    },

    {
      fieldName: 'remarks',
      label: '备注',
      component: 'Textarea',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '请输入备注',
      },
    },
  ];
}

/** 详情页字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      label: '人员ID，对应 per_employee.id',
      field: 'employeeId',
    },
    {
      label: '社会兼职单位名称',
      field: 'organizationName',
    },
    {
      label: '单位性质',
      field: 'organizationType',
      render: (val) =>
        h(
          'span',
          getDictOptions(DICT_TYPE.PER_EMPLOYER_TYPE).find(
            (opt) => opt.value === val,
          )?.label || val,
        ),
    },
    {
      label: '担任职务',
      field: 'position',
    },
    {
      label: '取酬情况',
      field: 'paymentStatus',
      render: (val) =>
        h(
          'span',
          getDictOptions(DICT_TYPE.PER_PART_TIME_PAYMENT_STATUS).find(
            (opt) => opt.value === val,
          )?.label || val,
        ),
    },
    {
      label: '兼职时间',
      field: 'partTimeDate',
      render: (val) => formatDate(val, 'YYYY-MM-DD') as string,
    },
    {
      label: '批准部门',
      field: 'approvedDepartment',
    },
    {
      label: '记录状态',
      field: 'status',
      render: (val) =>
        h(
          'span',
          getDictOptions(DICT_TYPE.COMMON_STATUS).find(
            (opt) => opt.value === val,
          )?.label || val,
        ),
    },
    {
      label: '备注',
      field: 'remarks',
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
    {
      label: '创建时间',
      field: 'createTime',
      render: (val) => formatDate(val, 'YYYY-MM-DD HH:mm:ss') as string,
    },
    {
      label: '更新时间',
      field: 'updateTime',
      render: (val) => formatDate(val, 'YYYY-MM-DD HH:mm:ss') as string,
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'organizationName',
      label: '单位名称',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入社会兼职单位名称',
      },
    },
    {
      fieldName: 'organizationType',
      label: '单位性质',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.PER_EMPLOYER_TYPE),
        placeholder: '请选择单位性质',
      },
    },
    {
      fieldName: 'partTimeDate',
      label: '兼职时间',
      component: 'DatePicker',
      componentProps: {
        showTime: false,
        format: 'YYYY-MM-DD',
        valueFormat: 'x',
        placeholder: '请选择兼职时间',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<PerPartTimeApi.PartTime>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '主键ID',
      minWidth: 120,
      visible: false,
    },
    {
      field: 'organizationName',
      title: '单位名称',
      minWidth: 120,
    },
    {
      field: 'organizationType',
      title: '单位性质',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.PER_EMPLOYER_TYPE },
      },
    },
    {
      field: 'position',
      title: '担任职务',
      minWidth: 120,
    },
    {
      field: 'paymentStatus',
      title: '取酬情况',
      minWidth: 120,
    },
    {
      field: 'partTimeDate',
      title: '兼职时间',
      minWidth: 120,
      formatter: 'formatDate',
    },
    {
      field: 'approvedDepartment',
      title: '批准部门',
      minWidth: 120,
    },
    {
      field: 'status',
      title: '记录状态',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },

    {
      field: 'actions',
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
